#include "l_block.h"
#include "ui_l_block.h"
#include <QPainter>
#include <QColor>
#include <iostream>
#include <QTimer>

L_Block::L_Block(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::L_Block)
{
    ui->setupUi(this);
    initColor();
}

L_Block::~L_Block()
{
    delete ui;
}
void L_Block::sleep(unsigned int msec) //延时器来自blog.fcsys.org
{
    QEventLoop eventloop;
    QTimer::singleShot(msec, &eventloop, SLOT(quit()));
    eventloop.exec();
}
void L_Block::showStart(int n){
    if(n==0){
            return;
        }else if(n==1){
            for(int i=0;i<2;i++){
                for(int j=0;j<2;j++){
                    mp[i][j]=t[i][j];
                }
            }
            this->repaint();
            sleep(500);
        }else{
            showStart(n-1);
            int val = (1<<(n-1));
            for(int i=0;i<val;i++){
                for(int j=0;j<val;j++){
                    if(mp[i][j]){
                        mp[val*2-1-i][j] = mp[i][j]+1;
          //              mp[i][val*2-1-j] = mp[i][j]+1;
          //              mp[val+i][val+j] = mp[i][j];
                    }
                }
            }
            this->repaint();
            sleep(500);
            for(int i=0;i<val;i++){
                for(int j=0;j<val;j++){
                    if(mp[i][j]){
          //             mp[val*2-1-i][j] = mp[i][j]+1;
                        mp[i][val*2-1-j] = mp[i][j]+1;
          //              mp[val+i][val+j] = mp[i][j];
                    }
                }
            }
            this->repaint();
            sleep(500);
            for(int i=0;i<val;i++){
                for(int j=0;j<val;j++){
                    if(mp[i][j]){
          //              mp[val*2-1-i][j] = mp[i][j]+1;
          //              mp[i][val*2-1-j] = mp[i][j]+1;
                        mp[val+i][val+j] = mp[i][j];
                    }
                }
            }
            this->repaint();
            sleep(500);

            mp[val-1][val-1]=n*2;
            mp[val-1][val]=n*2;
            mp[val][val-1]=n*2;
            this->repaint();
            sleep(500);
        }
}

void L_Block::on_spinBox_valueChanged(int arg1)
{
    std::cout << "value Change" << std::endl;
    this->ui->spinBox->setEnabled(false);
    memset(mp,0,sizeof(mp));
    showStart(this->ui->spinBox->value());
    this->ui->spinBox->setEnabled(true);
}
void L_Block::paintEvent(QPaintEvent*){
    paint=new QPainter;
    paint->begin(this);
    paint->setBrush(QBrush(Qt::SolidPattern));
    int tmp = this->ui->spinBox->value();
    int val = 1<<tmp;
    cell_size = block_max/val;
    for(int i=0;i<val;i++){
        for(int j=0;j<val;j++){
            paint->setBrush(*bgColor[mp[i][j]]);
            paint->drawRect(QRect(QPoint(i*cell_size+bor_x,j*cell_size+bor_y),QSize(cell_size,cell_size)));
        }
    }
}
void L_Block::initColor(){
    bgColor[0] = new QColor("#FFFFFF");
    bgColor[1] = new QColor("#FFFF00");
    bgColor[2] = new QColor("#0000FF");
    bgColor[3] = new QColor("#FF0000");
    bgColor[4] = new QColor("#00FFFF");
    bgColor[5] = new QColor("#FF00FF");
    bgColor[6] = new QColor("#00FF00");
    bgColor[7] = new QColor("#8B1A1A");
    bgColor[8] = new QColor("#1F1F1F");
    bgColor[9] = new QColor("#66CD00");
    bgColor[10] = new QColor("#ADD8E6");
    bgColor[11] = new QColor("#B3EE3A");
    bgColor[12] = new QColor("#3A5FCD");
    bgColor[13] = new QColor("#00CD66");
    bgColor[14] = new QColor("#1E90FF");
    bgColor[15] = new QColor("#7A67EE");
    bgColor[16] = new QColor("#8B5742");
    bgColor[17] = new QColor("#999999");
    bgColor[18] = new QColor("#DC143C");
    bgColor[19] = new QColor("#A4D3EE");
    bgColor[20] = new QColor("#8A2BE2");
}
